package com.terrace.core.mybatis.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
 * Long转Date类型转换器
 * @author jiangyg
 *
 */
public class LongToDateTypeHandler implements TypeHandler<Date> {

	/**
	 * JDBC参数设置值
	 */
	@Override
	public void setParameter(PreparedStatement ps, int i, Date date, JdbcType jdbcType) throws SQLException {
		ps.setLong(i, date.getTime());
	}

	/**
	 * 根据列名获取值
	 */
	@Override
	public Date getResult(ResultSet rs, String columnName) throws SQLException {
		Long value = rs.getLong(columnName);
		if (value != null && value != 0) {
			return new Date(value);
		}
		return null;
	}

	/**
	 * 根据第几列获取值
	 */
	@Override
	public Date getResult(ResultSet rs, int columnIndex) throws SQLException {
		Long value = rs.getLong(columnIndex);
		if (value != null && value != 0) {
			return new Date(value);
		}
		return null;
	}

	/**
	 * 存储过程转换
	 */
	@Override
	public Date getResult(CallableStatement cs, int columnIndex) throws SQLException {
		Long value = cs.getLong(columnIndex);
		if (value != null && value != 0) {
			return new Date(value);
		}
		return null;
	}

}
